<template>
  <el-form
    v-if="config.length>0"
    ref="filterForm"
    :inline="true"
    :model="filterModel"
    class="filter-container"
    size="small"
    @submit.native.prevent
  >
    <el-form-item
      v-for="(item,index) in config"
      :key="index"
      :prop="item.prop"
    >
      <!--输入框表单类型-->
      <el-input
        v-if="item.type ==='text'"
        v-model="filterData[item.prop]"
        :placeholder="item.placeholder?item.placeholder:item.label"
        @keyup.enter.native="handleFilter"
      >
        <i
          slot="suffix"
          class="el-input__icon el-icon-search"
          @click="handleFilter"
        />
      </el-input>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  name: 'FilterData',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    config: {
      type: Array,
      default() {
        return []
      }
    },
    filterData: {
      type: Object,
      default() {
        return {}
      }
    }
  },

  data() {
    return {
      filterModel: {}
    }
  },
  watch: {
    /* 实现表单数据的绑定，实时接收父组件的数据变化*/
    filterData() {
      this.filterModel = this.filterData
    }
  },
  mounted() {
    // 将组件上的属性赋值给当前组件内变量，因为props只能单向绑定,还需要监听属性值变化进行父子组件间交互
    this.filterModel = this.filterData
  },
  methods: {
    // 提交表单数据
    handleFilter() {
      this.$emit('filter', this.filterModel)
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.filter-container {
  .el-input {
    width: 100% !important;
  }

  .el-select {
    width: 100% !important;
  }

  .el-form-item {
    margin-bottom: 0;
  }
}
</style>
